package com.dindang.ddm.mvp.model.greedao;

import com.dindang.ddm.app.utils.DBUtils;
import com.dindang.ddm.app.utils.MyUtils;
import com.dindang.ddm.mvp.model.entity.RepairProgress;
import com.dindang.ddm.mvp.model.greedao.dao.RepairProgressDao;

import java.util.Date;
import java.util.List;

/**
 * Created by cxw on & 2018/11/9
 */
public class DataRepairProgressController {

    private static RepairProgressDao mRepairProgressDao = DBUtils.getDaoSession().getRepairProgressDao();
    /**
     * 增加报修进度
     */
    public static long insertRepairProgress(RepairProgress repairProgress)
    {
        repairProgress.setUserid(Long.parseLong(MyUtils.userid));
        repairProgress.setUsername(MyUtils.username);
        repairProgress.setIsMaintenancePlan("");
        repairProgress.setUpdateTime(new Date());
        return mRepairProgressDao.insertOrReplace(repairProgress);
    }

    /**
     * 查巡检列表
     */
    public static List<RepairProgress> selectByBillid(String billid)
    {
        //惰性加载
        List<RepairProgress> list = mRepairProgressDao.queryBuilder()
                .where(RepairProgressDao.Properties.Userid.eq(Long.parseLong(MyUtils.userid)))
                .where(RepairProgressDao.Properties.Billid.eq(billid))
                .orderDesc(RepairProgressDao.Properties.UpdateTime)
                .list();
        return list;
    }

    /**
     * 更新一条数据
     * @param repairProgress
     */
    public static void updateRepairProgress(RepairProgress repairProgress){
        mRepairProgressDao.update(repairProgress);
    }

    /**
     * 根据步骤查询一条记录
     */
    public static RepairProgress findByBillidAndStep(String billid, String step)
    {
        //惰性加载
        RepairProgress repairProgress = mRepairProgressDao.queryBuilder()
                .where(RepairProgressDao.Properties.Userid.eq(Long.parseLong(MyUtils.userid)))
                .where(RepairProgressDao.Properties.Billid.eq(billid))
                .where(RepairProgressDao.Properties.Step.eq(step))
                .unique();
        return repairProgress;
    }
}
